﻿@model CompanyManagementDLL.MetaData.TaskMetaData
@{
    ViewBag.Title = "Task Attachment";
}
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title">Task Attcahment</h3>
            </div>
            <div class="box-body table-responsive">
                <div class="form-group">
                    <label for="exampleInputFile">Add Document</label>
                    <input type="file" id="fileupload" kl_virtual_keyboard_secure_input="on" multiple="multiple">
                </div>
                <div class="form-group" style="width:20%">
                    @Html.TextBox("Detail","", new { @class = "form-control", @placeholder = "Enter Detail" })
                    <div id="error" style="display: none; color: red; margin-left: 2%">
                    </div>
                </div>
                <div class="form-group" style="width:20%">
                    <button class="btn btn-primary form-control" type="submit" id="submit" >Submit</button>
                </div>
                <div id="divAttach" style="margin-top: 2%">
                    @Html.HiddenFor(model => model.TaskID)

                    @Html.Action("loadAttachments", "Attachment", new { TaskID = Model.TaskID })
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $("#pagetitle").html("Task Attachment");

    var files;
    $('#fileupload').on('change', function (e) {
        files = e.target.files;
        var filename = $(this).val();
        var data = new FormData();
        for (var i = 0; i < files.length; i++) {
            if (window.FormData != undefined) {
                data.append("file" + 1, files[i]);
                var request = new XMLHttpRequest();
                request.open("POST", "/Attachment/UploadAttachment");
                request.send(data);
            }
        }

    });

    $("#submit").click(function () {
        var detail = $("#Detail").val();
        if (detail != '') {
            $("#error").empty();
            var astatch = {
                TaskID: $("#TaskID").val(),
                Path: files.item(0).name,
                Detail: $("#Detail").val()
            };

            $.ajax({
                url: "/Attachment/Upload",
                contentType: "application/json",
                type: "POST",
                datatype: "json",
                data: JSON.stringify({ astatch: astatch }),
                success: function (data) {
                    $("#divAttach").load("/Attachment/loadAttachments?TaskID=" + $("#TaskID").val());

                    $('.dataTable').dataTable({
                        "bPaginate": true,
                        "bLengthChange": true,
                        "bFilter": true,
                        "bSort": true,
                        "bInfo": true,
                        "bAutoWidth": false
                    });
                }
            });
        }
        else {
            $("#error").html("Detail is Required.");
            $("#error").show();
        }
    });
</script>

